package les.projeto;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class LocalView extends Activity{
	
	private String LocalBuilding = "";
	private String LocalRoom = "";
	private String LocalReference = "";
	private String[][] aboutLocal = null;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.local);
	    
	    updateListValues();
	    ListView local = (ListView)findViewById(R.id.listViewLocal);
	    local.setOnItemClickListener(new OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view,
	    		int position, long id) {
	    		showLocal(aboutLocal[position][0], aboutLocal[position][1], aboutLocal[position][2]);
	    	}
	    });
	    
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	     Resources st = getResources();
         super.onCreateOptionsMenu(menu);
         menu.add(0,0,0,getString(R.string.addLocalText));
         return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         super.onOptionsItemSelected(item);
         switch (item.getItemId()) {
            case 0:
                addBuildingLocal();
                break;
            case 1:
         }
         return false;
    } 
	
	private void updateListValues() {
	    ListView local = (ListView)findViewById(R.id.listViewLocal);

	    BD bd = new BD();
	    ArrayList<String> locs = bd.getLocal(this);
	    
	    aboutLocal = new String[locs.size()][3];
	    
	    for (int i = 0; i < locs.size(); i++) {
			String[] date = locs.get(i).split(" ");
			aboutLocal[i][0] = date[0];
			aboutLocal[i][1] = date[1];
			aboutLocal[i][2] = date[2];
		}
	    
	    String[] values = new String[aboutLocal.length];
	    for (int i = 0; i < values.length; i++) {
			values[i] = "Predio: " + aboutLocal[i][0] + " - Sala: " + aboutLocal[i][1];
		}
	    
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		android.R.layout.simple_list_item_1, android.R.id.text1, values);
	    local.setAdapter(adapter);
	    local.invalidate();
	}

	private void showLocal(final String building, final String room, final String reference) {
		 final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(building);
		 helpBuilder.setMessage("Room: " + room + "\nReference: " + reference);
		 helpBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  editLocalBuilding(building, room, reference);
				  dialog.cancel();
			  }
		 });
		 helpBuilder.setNegativeButton("Back", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  dialog.cancel();
			  }
			 });
		 helpBuilder.setNeutralButton("remove", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  BD bd = new BD();
				  bd.removeLocal(LocalView.this, building);
				  dialog.cancel();
				  startActivity(getIntent()); 
				  finish();
			  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		}
		

	private void addBuildingLocal() {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Add place");
		 helpBuilder.setMessage("Enter name place: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText("");
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  LocalBuilding = "";
				  LocalBuilding = input.getText().toString();
				  dialog.cancel();
				  addRoomLocal();
			  }
		 });
		 helpBuilder.setNeutralButton("Back", new DialogInterface.OnClickListener() {
		  @Override
		  public void onClick(DialogInterface dialog, int which) {
			  dialog.cancel();
		  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
	}

	protected void addRoomLocal() {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Add place");
		 helpBuilder.setMessage("Enter room: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText("");
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  LocalRoom = "";
				  LocalRoom = input.getText().toString();
				  dialog.cancel();
				  addReferenceLocal();
			  }
		 });
		 helpBuilder.setNeutralButton("Back", new DialogInterface.OnClickListener() {
		  @Override
		  public void onClick(DialogInterface dialog, int which) {
			  dialog.cancel();
		  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		
	}


	protected void addReferenceLocal() {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Add local");
		 helpBuilder.setMessage("Enter a reference site of the building: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText("");
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  LocalReference = "";
				  LocalReference = input.getText().toString();
				  dialog.cancel();
				  addLocalInBD();
			  }
		 });
		 helpBuilder.setNeutralButton("Back", new DialogInterface.OnClickListener() {
		  @Override
		  public void onClick(DialogInterface dialog, int which) {
			  dialog.cancel();
		  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
	}
	
	protected void editLocalBuilding(final String building, final String room, final String reference) {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Editing a place " + building);
		 helpBuilder.setMessage("Enter the building ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText(building);
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  LocalBuilding = "";
				  LocalBuilding = input.getText().toString();
				  editLocalRoom(building, room, reference);
				  dialog.cancel();
			  }
		 });
		 helpBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
		  @Override
		  public void onClick(DialogInterface dialog, int which) {
			  dialog.cancel();
		  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();		
	}
	
	protected void editLocalRoom(final String building, String room, final String reference) {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Editing place " + building);
		 helpBuilder.setMessage("Enter the new room: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText(room);
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  LocalRoom = "";
				  LocalRoom = input.getText().toString();
				  dialog.cancel();
				  editLocalReference(building, reference);
			  }
		 });
		 helpBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
		  @Override
		  public void onClick(DialogInterface dialog, int which) {
			  dialog.cancel();
		  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();
		
	}
	
	protected void editLocalReference(final String building, String reference) {
		 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle("Editing place " + building);
		 helpBuilder.setMessage("Enter a new reference: ");
		 final EditText input = new EditText(this);
		 input.setSingleLine();
		 input.setText(reference);
		 helpBuilder.setView(input);
	
		 helpBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
			  @Override
			  public void onClick(DialogInterface dialog, int which) {
				  LocalReference = "";
				  LocalReference = input.getText().toString();
				  BD bd = new BD();
				  bd.editLocal(LocalView.this, building,LocalBuilding, LocalRoom, LocalReference);
				  updateListValues();
				  dialog.cancel();
			  }
		 });
		 helpBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
		  @Override
		  public void onClick(DialogInterface dialog, int which) {
			  dialog.cancel();
		  }
		 });
		
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();		
	}
	protected void addLocalInBD() {
	    BD bd = new BD();
	    bd.insertLocal(this, LocalBuilding, LocalRoom, LocalReference);
	    startActivity(getIntent()); 
	    finish();
	}
	
	
	
	
	@Override
	public void onBackPressed() {
		
        back();	        		 
        return;
	}

	private void back() {
		Calendar date = Calendar.getInstance();
		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        int today = date.get(Calendar.DAY_OF_MONTH);
        
	    Intent trocatela = new Intent(LocalView.this, CalendarView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+today);

		LocalView.this.startActivity(trocatela);
		LocalView.this.finish();
	}
	
	
}

